/**
*  \file control_main_contactor.c
*
*  \brief
*
*  \author Michael J. Hohmann  <michael.hohmann@linde-mh.de>
*
*
*/

/*===============================================[ private includes  ]================================================*/
#include "control_main_contactor.h"
#include "../assertor/assertor.h"
#include "../pal/pal_mem.h"
#include "../interpol/interpol_u16_u16.h"
#include "../contactor/contactor.h"
/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/** Private variables */
struct control_main_contactor_private
{
	struct contactor_obj main_contactor;
};

/*===============================================[ private variables ]================================================*/
static struct control_main_contactor_private control_main_contactor;

static const U16 tighten_x[]= {U16_MIN,   35,       36,       60,      61,     U16_MAX};
static const U16 tighten_y[]= {0,          0,      65535,   52428,     0,     0      };

static const U16 hold_x[]   = {U16_MIN,   35,        36,       60,        61,     U16_MAX};
static const U16 hold_y[]   = {0,          0,       41690,     26214,       0,      0    };


static const struct interpol_u16_u16_obj pwm_tighten={ 6, tighten_x, tighten_y };
static const struct interpol_u16_u16_obj pwm_hold   ={ 6, hold_x, hold_y };


/*===============================================[ private functions ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
RC control_main_contactor_initialize( void )
{
   pal_memzero( &control_main_contactor, sizeof control_main_contactor);

   contactor_construct( &control_main_contactor.main_contactor,&pwm_tighten, &pwm_hold, 100 );

   return RC_SUCCESS;
}

U16 control_main_contactor_10ms( BO value,S16 preChargred, F32 dc_link_voltage, F32 key_voltage )
{

    BO contactor_state=FALSE;

    if( value && (preChargred >= 1 )&&( dc_link_voltage > 38 ))
    {
        contactor_state=TRUE;
    }
	else
	{
	    control_main_contactor_initialize();
		contactor_state=FALSE;
	}
    
    return contactor_service( &control_main_contactor.main_contactor, key_voltage , contactor_state );

   // return contactor_service( &control_main_contactor.main_contactor, key_voltage , TRUE);
} 

/*===============================================[ private functions ]================================================*/


/***********************************************************************************************************************
*                             Copyright 2020 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
